package com.it.homrwork;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

/**
 * @author げっしょく
 * @date 2021/8/10
 */
public class Test1_2 implements Runnable{
    private Socket s;
    public Test1_2(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            bw = new BufferedWriter(new FileWriter("Day17//" + UUID.randomUUID().toString() + ".txt"));
            String line ;
            while ((line = br.readLine()) != null){
                bw.write(line);
                bw.newLine();
            }
            bw.flush();
            bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            //OutputStream os = s.getOutputStream();
            //os.write("上传成功".getBytes());
            bw.write("上传成功");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
